/**
 * 
 */
package com.base.myproject.client.busi.system;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.SendServlet;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BasePagingLoader;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;

/**
 * 审批前台，人员登录后进行审批的界面
 * @author zgw
 *2009-9-7 下午04:42:31
 * javazgw@gmail.com
 */
public class Ratify extends ContentPanel{
	BasePagingLoader< PagingLoadResult<BusiObject>>  loder ;
	GroupingStore<BusiObject> groupstore;
	SelectionListener<ButtonEvent> listener;
	Grid grid;
	public Ratify()
	{
		init();
	}
	void init()
	{
		createscript();
		TableLayout tl = new TableLayout(5);
		
		this.setLayout(tl);
		setHeight("700px");
		setWidth("820px");
		initListener();
		initstore();
		initLeftPanel();
		initWidget();
	}
	void initstore()
	{
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("busicode");
		type.addField("ratifybcode");
		type.addField("bcode");
		type.addField("fromstatus");
		type.addField("tostatus");
		type.addField("ratifystatus");
		type.addField("busiinnercode");
		type.addField("icode");
		type.addField("ratifystartdate");
		StoreInfo info = new StoreInfo();

		info.setTablename("ratifydetail");
		info.setSql("select * from ratifydetail where ratifybcode='999' order by ratifystartdate");
		info.setKeyname(new String[]{"icode"});
		SendServlet ss = new SendServlet();
		loder = ss.getBaseLoaderX(type,info);
		 groupstore = new GroupingStore<BusiObject>(loder);  
		 loder.load();
		 groupstore.groupBy("ratifystartdate");  
//		 loder.addLoadListener(new LoadListener(){
//			 public void loaderLoad(LoadEvent e){
//				 System.out.println("group ............");
//			 }
//		 });
	}
	void initWidget()
	{
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig bcode = new ColumnConfig("bcode", "单据所属部门人员", 100);

		columns.add(bcode);
		
		
		ColumnConfig busicode = new ColumnConfig("busicode", "单据类型", 100);

		columns.add(busicode);
		
		ColumnConfig fromstatus = new ColumnConfig("fromstatus", "单据状态", 100);

		columns.add(fromstatus);
		
		
		ColumnConfig tostatus = new ColumnConfig("tostatus", "单据审批后状态", 100);

		columns.add(tostatus);
	
		
		ColumnConfig ratifystartdate = new ColumnConfig("ratifystartdate", "提交审批日期", 100);

		columns.add(ratifystartdate);
	
		
		
	
		final ColumnModel cm = new ColumnModel(columns);
		
		
		  GroupingView view = new GroupingView();  
		    view.setShowGroupedColumn(false);  
		    view.setForceFit(true);  
		    view.setGroupRenderer(new GridGroupRenderer() {  
		      public String render(GroupColumnData data) {  
		        String f = cm.getColumnById(data.field).getHeader();  
		        String l = data.models.size() == 1 ? "Item" : "Items";  
		        return f + ": " + data.group + " (" + data.models.size() + " " + l + ")";  
		      }  
		    });  
		 grid = new Grid(groupstore, cm);  
		cbs.setSelectionMode(SelectionMode.MULTI);
//		grid.getSelectionModel().setSelectionMode(SelectionMode.MULTI);  
		grid.setSelectionModel(cbs);
		
		grid.setView(view);
		grid.setAutoHeight(true);
	//	grid.setWidth("400px");
		grid.setAutoWidth(true);
		TableData td = new TableData();
		td.setColspan(4);
		td.setVerticalAlign(VerticalAlignment.TOP);
		ContentPanel cp = new ContentPanel();
		cp.setFrame(false);
		cp.setHeading("用户下订单商品列表");
		cp.setStyleName("basepanelmargin");
		cp.add(grid);
		cp.setAutoWidth(true);
		add(cp,td);
	}
	void initLeftPanel()
	{
		ContentPanel cp = new ContentPanel();
		cp.setHeading("分组显示方式");
		cp.setStyleName("basepanelmargin");
		cp.setHeaderVisible(true);
		cp.setFrame(false);
		VerticalPanel vp = new VerticalPanel();
		
		Button groupbyratifystartdate = new Button("提交审批日期分组");
		
		
		Button groupbybcode = new Button("部门人员分组");
		Button groupbybusicode = new Button("单据类型分组");
		Button groupbystatus = new Button("单据状态分组");
		
		
		groupbyratifystartdate.setId("groupbyratifystartdate");
		groupbybcode.setId("groupbybcode");
		groupbybusicode.setId("groupbybusicode");
		groupbystatus.setId("groupbystatus");
		groupbyratifystartdate.setWidth("130px");
		groupbybcode.setWidth("130");
		groupbybusicode.setWidth("130px");
		groupbystatus.setWidth("130px");
		vp.add(groupbyratifystartdate);
		vp.add(groupbybcode);
		vp.add(groupbybusicode);
		vp.add(groupbystatus);
		groupbyratifystartdate.addSelectionListener(listener);
		groupbybcode.addSelectionListener(listener);
		groupbybusicode.addSelectionListener(listener);
		groupbystatus.addSelectionListener(listener);
		TableData td = new TableData();
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setColspan(1);
//		add(vp,td);
		vp.add(initExcPanel());
		cp.add(vp);
		add(cp,td);
	}
	ContentPanel initExcPanel()
	{
		ContentPanel cp = new ContentPanel();
		cp.setHeading("操作");
		cp.setStyleName("basepanelmargin");
		cp.setHeaderVisible(true);
		cp.setFrame(false);
		cp.setBodyBorder(false);
		VerticalPanel vp = new VerticalPanel();
		
		Button ratifyyes = new Button("审批通过");
		ratifyyes.setId("ratifyyes");
		Button ratifyno = new Button("审批不通过");
		ratifyno.setId("ratifyno");
//		Button shipsendcancel = new Button("");
//		Button shipping = new Button("安排发货");

		ratifyyes.setWidth("130px");
//		shipping.setWidth("130px");
		ratifyno.setWidth("130px");
//		shipsendcancel.setWidth("130px");
		vp.add(ratifyyes);
		vp.add(ratifyno);
//		vp.add(shipsendcancel);
//		vp.add(shipping);


		ratifyyes.addSelectionListener(listener);
//		shipping.addSelectionListener(listener);
//		shipsendcancel.addSelectionListener(listener);
		ratifyno.addSelectionListener(listener);

		cp.add(vp);
		
		return cp;
	}
	void initListener()
	{
		
		 listener = new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				System.out.println(ce.getButton().getText());
				System.out.println(ce.getButton().getId());
			
				
				
				if(ce.getButton().getId().equals("groupbyratifystartdate"))
				{
					groupstore.groupBy("ratifystartdate");	
				}
				else if(ce.getButton().getId().equals("groupbybcode"))
				{
					groupstore.groupBy("bcode");	
				}
				else if(ce.getButton().getId().equals("groupbybusicode"))
				{
					groupstore.groupBy("busicode");	
				}
				else if(ce.getButton().getId().equals("groupbystatus"))
				{
					groupstore.groupBy("fromstatus");	
				}
				else if(ce.getButton().getId().equals("ratifyyes"))
				{
					
				}
				else if(ce.getButton().getId().equals("ratifyno"))
				{
					
				}
			
				
			}
		 };
	}
	private native void createscript()/*-{
	
}-*/;
}
